// 我的消息
import { navigation } from "../../../utils/commons"

Page({
  data: {
    selectItems: [
      {
        name: "全部",
        checked: false,
      },
      {
        name: "我收到的",
        checked: false,
      },
      {
        name: "我发出的",
        checked: false,
      },
      {
        name: "申请",
        checked: false,
      },
      {
        name: "邀请",
        checked: false,
      },
    ],
    selectedName: "全部",
    list: "",
    chuangNum: "",
    sessionID: "",

    // request 参数
    parameters: {
      pageParam: {
        pageSize: 1000,
        pageNum: 1,
        orderBy: "id asc",
      },
      type: "ALL",
    },
  },

  onLoad() {
    this.setData({
      "selectItems[0].checked": true,
      sessionID:
        wx.getStorageSync(
          "sessionID"
        ),
      chuangNum:
        wx.getStorageSync(
          "chuangNum"
        ),
    })

    this.request()
  },

  setList(list) {
    // console.log("list", list);
    this.setData({
      list: "",
    })
    for (let i of list) {
      // console.log("i", i);
      let status = i.status,
        memChuangNum =
          i.memberChuangNum,
        myChuangNum =
          this.data.chuangNum,
        memName =
          i.memberNickname,
        captainName =
          i.projectCaptainNickname,
        type = i.type
      let statusName = "",
        message = ""
      // 待处理
      if (status === 0) {
        statusName = "待处理"
        // 待处理申请
        if (type === "申请") {
          // 我发出的申请
          if (
            memChuangNum ===
            myChuangNum
          ) {
            message =
              "您的入队申请待回复"
            // message = memName + '申请参加您的队伍';
          }
          // 我收到的申请
          else {
            message =
              memName +
              "申请参加您的队伍"
            // message = '您的入队申请待处理';
          }
        }
        // 待处理邀请
        else {
          // 我收到的邀请
          if (
            memChuangNum ===
            myChuangNum
          ) {
            message =
              captainName +
              "邀请您参加队伍"
          }
          // 我发出的邀请
          else {
            message =
              "您的入队邀请待回复"
          }
        }
      }
      // 已拒绝
      else if (
        status === -1
      ) {
        statusName = "已拒绝"
        // 已拒绝申请
        if (type === "申请") {
          // 我发出的申请
          if (
            memChuangNum ===
            myChuangNum
          ) {
            message =
              "您的入队申请被拒绝"
          }
          // 我收到的申请
          else {
            message =
              "您已拒绝" +
              memName +
              "的入队申请"
          }
        }
        // 已拒绝邀请
        else {
          // 我收到的邀请
          if (
            memChuangNum ===
            myChuangNum
          ) {
            message =
              "您已拒绝" +
              captainName +
              "的入队邀请"
          }
          // 我发出的邀请
          else {
            message =
              memName +
              "已拒绝您的入队邀请"
          }
        }
      }
      // 已通过
      else {
        statusName = "已通过"
        // 已通过申请
        if (type === "申请") {
          // 我发出的申请
          if (
            memChuangNum ===
            myChuangNum
          ) {
            message =
              "您的入队申请已通过"
          }
          // 我收到的申请
          else {
            message =
              "您已同意" +
              memName +
              "的入队申请"
          }
        }
        // 已通过邀请
        else {
          // 我收到的邀请
          if (
            memChuangNum ===
            myChuangNum
          ) {
            message =
              "您已接受" +
              captainName +
              "的入队邀请"
          }
          // 我发出的邀请
          else {
            message =
              memName +
              "已接受您的入队邀请"
          }
        }
      }
      i.statusName =
        statusName
      i.message = message
      this.setData({
        list: list,
      })
    }
  },

  request() {
    // 获取后端数据，显示消息列表
    wx.request({
      url: "https://www.hschuangsai.com/message/list",
      method: "POST",
      data: this.data
        .parameters,
      header: {
        session:
          this.data.sessionID,
      },
      success: res => {
        // console.log("data", this.data.parameters);
        // console.log("request success", res);
        this.setList(
          res.data.data.list
        )
      },
    })
  },

  // 选择消息类别
  radioChange(e) {
    let index = e.detail.value
    this.setData({
      selectedName:
        this.data.selectItems[
          index
        ].name,
    })
    switch (
      this.data.selectedName
    ) {
      case "我收到的":
        this.setData({
          "parameters.type":
            "RECIEVE",
        })
        break
      case "我发出的":
        this.setData({
          "parameters.type":
            "SEND",
        })
        break
      case "申请":
        this.setData({
          "parameters.type":
            "APPLICATION",
        })
        break
      case "邀请":
        this.setData({
          "parameters.type":
            "INVITATION",
        })
        break
      default:
        this.setData({
          "parameters.type":
            "ALL",
        })
    }
  },

  showModal(e) {
    this.setData({
      modalName:
        e.currentTarget
          .dataset.target,
    })
  },

  hideModal(e) {
    this.setData({
      modalName: null,
    })
    this.request()
  },

  // 查看详情页
  goDetail(e) {
    let item =
        this.data.list[
          e.currentTarget
            .dataset.index
        ],
      myChuangNum =
        this.data.chuangNum
    console.log("item", item)
    // 待处理
    if (item.status === 0) {
      // 待处理申请
      if (
        item.type === "申请"
      ) {
        // 我发出的申请
        if (
          item.memberChuangNum ===
          myChuangNum
        ) {
          console.log(
            "我发出的申请待回复"
          )
          navigation(
            "/pages/recruit/introduction/introduction?idOfPrg=" +
              item.projectId
          )
        }
        // 我收到的申请
        else {
          console.log(
            "我收到的申请待处理"
          )
          navigation(
            "../../project/applicationInDetail/applicationInDetail?ChuangNum=" +
              item.memberChuangNum +
              "&status=" +
              item.status +
              "&messageId=" +
              item.id
          )
        }
      }
      // 待处理邀请
      else {
        // 我收到的邀请
        if (
          item.memberChuangNum ===
          myChuangNum
        ) {
          console.log(
            "我收到的邀请待处理"
          )
          navigation(
            "../message/invitation/invitation?idOfPrg=" +
              item.projectId +
              "&msgId=" +
              item.id
          )
        }
        // 我发出的邀请
        else {
          navigation(
            "/pages/recruit/introduction/introduction?idOfPrg=" +
              item.projectId
          )
        }
      }
    }
    // 已拒绝
    else if (
      item.status === -1
    ) {
      // 已拒绝申请
      if (
        item.type === "申请"
      ) {
        // 我发出的申请
        if (
          item.memberChuangNum ===
          myChuangNum
        ) {
          console.log(
            "我发出的申请被拒绝"
          )
          navigation(
            "../message/checkReject/checkReject?id=" +
              item.id
          )
        }
        // 我收到的申请
        else {
          console.log(
            "我收到的申请已拒绝"
          )
          navigation(
            "../message/checkReject/checkReject?id=" +
              item.id
          )
        }
      }
      // 已拒绝邀请
      else {
        // 我收到的邀请
        if (
          item.memberChuangNum ===
          myChuangNum
        ) {
          console.log(
            "我收到的邀请已拒绝"
          )
          navigation(
            "../message/checkReject/checkReject?id=" +
              item.id
          )
        }
        // 我发出的邀请
        else {
          console.log(
            "我发出的邀请被拒绝"
          )
          navigation(
            "../message/checkReject/checkReject?id=" +
              item.id
          )
        }
      }
    }
    // 已通过
    else {
      // 已通过申请
      if (
        item.type === "申请"
      ) {
        // 我发出的申请
        if (
          item.memberChuangNum ===
          myChuangNum
        ) {
          console.log(
            "我发出的申请已通过"
          )
          navigation(
            "/pages/recruit/introduction/introduction?idOfPrg=" +
              item.projectId
          )
        }
        // 我收到的申请
        else {
          console.log(
            "我收到的申请已同意"
          )
          navigation(
            "/pages/recruit/introduction/introduction?idOfPrg=" +
              item.projectId
          )
        }
      }
      // 已通过邀请
      else {
        // 我收到的邀请
        if (
          item.memberChuangNum ===
          myChuangNum
        ) {
          console.log(
            "我收到的邀请已同意"
          )
          navigation(
            "/pages/recruit/introduction/introduction?idOfPrg=" +
              item.projectId
          )
        }
        // 我发出的邀请
        else {
          console.log(
            "我发出的邀请已通过"
          )
          navigation(
            "/pages/recruit/introduction/introduction?idOfPrg=" +
              item.projectId
          )
        }
      }
    }
  },
})
